package com.googlecode.stockhamster.arbiter;

import java.net.MalformedURLException;

import com.googlecode.stockhamster.Constant;
import com.googlecode.stockhamster.bank.IBank;
import com.googlecode.stockhamster.stockmarket.IStockMarket;
import com.googlecode.stockhamster.utils.XFireStub;

public class Arbiter implements IArbiter {

	private IBank bank;
	private IStockMarket market;

	public Arbiter() throws MalformedURLException {
		bank = new XFireStub<IBank>(Constant.bankUrl, IBank.class).getStub();
		market = new XFireStub<IStockMarket>(Constant.stockMarketUrl,
				IStockMarket.class).getStub();
	}

	@Override
	public Long getBalance(String loginCode) {
		Account ac = Authenticator.getAccount(loginCode);
		return bank.getBalance(ac.getUsername(), ac.getBankCode());
	}

	@Override
	public Integer getStockAmount(String loginCode, Integer stockId) {
		Account ac = Authenticator.getAccount(loginCode);
		return market.getStockAmount(ac.getUsername(), ac.getStockCode(),
				stockId);
	}

	@Override
	public Integer[] getStockList(String loginCode) {
		Account ac = Authenticator.getAccount(loginCode);
		System.out.println("Arbiter getStockList");
		return market.getStockList(ac.getUsername(), ac.getStockCode());
	}

	@Override
	public String login(String username, String password) {
		return Authenticator.login(username, password);
	}

	@Override
	public Boolean requestBuy(String loginCode, Integer stockId, Long price,
			Integer amount) {
		Account ac = Authenticator.getAccount(loginCode);
		Long moneyAmount = price * amount;
		String moneyCode = bank.cash(ac.getUsername(), ac.getBankCode(),
				moneyAmount);
		if (moneyCode == null)
			return false;
		return market.requestBuy(ac.getUsername(), ac.getStockCode(), stockId,
				price, amount, moneyCode);
	}

	@Override
	public Boolean requestSell(String loginCode, Integer stockId, Long price,
			Integer amount) {
		Account ac = Authenticator.getAccount(loginCode);
		return market.requestSell(ac.getUsername(), ac.getStockCode(), stockId,
				price, amount);
	}

	@Override
	public Boolean close(String loginCode) {
		Boolean ret = true;
		Account ac = Authenticator.getAccount(loginCode);
		ret = ret && bank.close(ac.getUsername(), ac.getBankCode());
		ret = ret && market.close(ac.getUsername(), ac.getStockCode());
		ret = ret && ac.delete();
		return ret;
	}

	@Override
	public Boolean open(String username, String password) {
		String bankCode = bank.open(username);
		if (bankCode == null)
			return false;
		String stockCode = market.open(username);
		if (stockCode == null) {
			bank.close(username, bankCode);
			return false;
		}
		Account ac = new Account();
		ac.setUsername(username);
		ac.setPassword(password);
		ac.setBankCode(bankCode);
		ac.setStockCode(stockCode);
		if (!ac.insert()) {
			bank.close(username, bankCode);
			market.close(username, stockCode);
			return false;
		}
		return true;
	}

	@Override
	public String cash(String loginCode, Long amount) {
		Account ac = Authenticator.getAccount(loginCode);
		return bank.cash(ac.getUsername(), ac.getBankCode(), amount);
	}

	@Override
	public Boolean cleanRequestList(String loginCode) {
		Account ac = Authenticator.getAccount(loginCode);
		return market.removeUserRequest(ac.getUsername(), ac.getStockCode());
	}
}
